package com.socialsoftware.MediaCommunication.service;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.media.projection.MediaProjection;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.socialsoftware.R;

/**
 * Service for handling screen capture
 * Maintains the foreground service required for screen sharing
 */
public class ScreenCaptureService extends Service {
    private static final String TAG = "ScreenCaptureService";
    private static final String CHANNEL_ID = "screen_share_channel";
    private MediaProjection mediaProjection;
    private final IBinder binder = new LocalBinder();
    private Intent mediaProjectionIntent; // 保存 Intent

    public class LocalBinder extends Binder {
        public ScreenCaptureService getService() {
            return ScreenCaptureService.this;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "ScreenCaptureService created");
        createNotificationChannel();
    }

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "Screen Sharing",
                    NotificationManager.IMPORTANCE_LOW
            );
            channel.setDescription("Screen sharing is active");
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(channel);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "ScreenCaptureService onStartCommand");
        
        if (intent != null) {
            mediaProjectionIntent = intent.getParcelableExtra("data");
            if (mediaProjectionIntent != null) {
                Log.d(TAG, "Received media projection intent");
            } else {
                Log.e(TAG, "Media projection intent is null in onStartCommand");
            }
        } else {
            Log.e(TAG, "Intent is null in onStartCommand");
        }

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("屏幕共享中")
                .setContentText("正在共享屏幕内容")
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setPriority(NotificationCompat.PRIORITY_LOW)
                .build();

        // 添加版本兼容性处理
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            startForeground(1, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION);
        } else {
            startForeground(1, notification);
        }

        return START_STICKY;
    }

    public Intent getMediaProjectionIntent() {
        if (mediaProjectionIntent == null) {
            Log.e(TAG, "Media projection intent is null when requested");
        } else {
            Log.d(TAG, "Returning media projection intent");
        }
        return mediaProjectionIntent;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "ScreenCaptureService onBind");
        return binder;
    }
    
    @Override
    public void onDestroy() {
        Log.d(TAG, "ScreenCaptureService destroyed");
        super.onDestroy();
    }
} 